import 'dart:ffi';
import 'package:flutter/services.dart';
import 'package:rtk_ota_plugin/rtk_ota_plugin.dart';
import 'package:rtk_ota_plugin/rtk_status.dart';
import 'rtk_ota_plugin_platform_interface.dart';



class MethodChannelRtkOtaPlugin extends RtkOtaPluginPlatform {

  // @visibleForTesting
  static const methodChannel = MethodChannel('rtk_ota_plugin');

  static BasicMessageChannel messageChannel = const BasicMessageChannel("rtk_ota_message", StringCodec());

  @override
  Future<String?> getPlatformVersion() async {
    final version = await methodChannel.invokeMethod<String>('getPlatformVersion');
    return version;
  }

  @override
  void initDfuAdapter(String address,String path) {
    methodChannel.invokeMethod('initDfuAdapter',{'address':address,
    'path':path});
  }

  @override
  void initIosDfuAdapter(String name,String path) {
    methodChannel.invokeMethod('initIosDfuAdapter',{'name':name,
      'path':path});
  }

  @override
  Future<Void> setAddress(String address) async {
    return await methodChannel.invokeMethod('setAddress',{'address':address});
  }

  @override
  void setFilePath(String path) async {
    return await methodChannel.invokeMethod('setFilePath',{'path':path});
  }

  @override
  Future<String> getColorPath() async {
    return await methodChannel.invokeMethod('getColorPath');
  }

  @override
  Future<String> getFirmwarePath() async {
    return await methodChannel.invokeMethod('getFirmwarePath');
  }

  @override
  Future<String> connectRemoteDevice(String address) async {
    return await methodChannel.invokeMethod('connectRemoteDevice',{'address':address});
  }

  @override
  void otaListener(Future<dynamic> Function(MethodCall call)? handler){
    methodChannel.setMethodCallHandler(handler);
  }

  @override
  Future<String> md5(String path)async{
    return await methodChannel.invokeMethod('md5',{'path':path});
  }

  @override
  void otaListenerMessage(RtkOtaPluginCallBack callBack){
    messageChannel.setMessageHandler((message){
      String data = message.toString();
      String d1 = data.split(",")[0];
      // String d2 = data.split(",")[1];
      if(d1 == "1"){
        callBack(RtkStatus.STATUS_CONNECT_SUCC,0);
      }
      if(d1 == "2"){
        callBack(RtkStatus.STATUS_CONNECT_ERROR,0);
      }
      if(d1 == "3"){
        callBack(RtkStatus.STATUS_READY_FINISH,0);
      }
      if(d1 == "4"){
        callBack(RtkStatus.STATUS_OTA_READYING,0);
      }
      if(d1 == "5"){
        callBack(RtkStatus.STATUS_OTA_FINISH,0);
      }
      if(d1 == "6"){
        String d2 = data.split(",")[1];
        callBack(RtkStatus.STATUS_UPGRADING,int.parse(d2));
      }
      if(d1 == "7"){
        callBack(RtkStatus.STATUS_UPGRAD_ERROR,0);
      }
      if(d1 == "8"){
        callBack(RtkStatus.STATUS_UPGRAD_SUCC,0);
      }
      if(d1 == "9"){
        callBack(RtkStatus.STATUS_DEVICE_DIS,0);
      }
      if(d1 == "10"){
        callBack(RtkStatus.STATUS_MAC_NONE,0);
      }
      if(d1 == "11"){
        callBack(RtkStatus.STATUS_DEVICE_CONNECTING,0);
      }
      if(d1 == "12"){
        callBack(RtkStatus.STATUS_MAC_NO_SELECT_FILE,0);
      }
      return callBack(RtkStatus.STATUS_ERROR,0);
      // return message;
    });
  }
  @override
  void disMessage(){
    messageChannel.setMessageHandler(null);
  }

}
